//= require jquery-ui-1.9.1.custom.min

$(document).ready(function(){

  var transactionTable = $('#transactions').dataTable( {
          "bPaginate": false,
          "bLengthChange": false,
          "bFilter": false,
          "bInfo": false,
          "bAutoWidth": false,
          "aaSorting": [[2,'desc']]
        });
  retroFitTransactionForm();

  updateTransactionTotal()

});


  function retroFitTransactionForm(){

   $("#transaction_on_date").datepicker({
     dateFormat: 'yy-mm-dd'
   });
   $("#transaction_on_date").datepicker("setDate", new Date());

   $("#submit_transaction").click(function(){
     location.reload();
   });

  
  }

function updateTransactionTotal(){
  var balance = parseFloat($('#initial_balance').text()); 
  //$($("#transactions > tbody > tr").get()).each(function() {
  $($("#transactions > tbody > tr").get().reverse()).each(function() {
    var credit_text = parseFloat($(this).find('.credit').text())
    var debit_text = parseFloat($(this).find('.debit').text())
    var credit = isNaN(credit_text)?0:parseFloat(credit_text)
    var debit = isNaN(debit_text)?0:parseFloat(debit_text)
    balance = balance + credit - debit
    $(this).find('.time_total').text(balance)
   });
}

function setCreateTransactionType(giver_type,giver_id,receiver_type,receiver_id){
  $('#transaction_giver_type').val(giver_type);
  $('#transaction_giver_id').val(giver_id);
  $('#transaction_receiver_type').val(receiver_type);
  $('#transaction_receiver_id').val(receiver_id);
}
